package  com.example.cursor_animation

import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import javax.swing.JComponent
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.ui.jcef.JBCefBrowser
import com.intellij.ui.jcef.JBCefClient

class MyWebView {
    fun createComponent(): JComponent {
        val  baseUrl = "https://leetcode.cn"

        // 创建 JBCefBrowser 实例，并设置初始网页
        val browser = JBCefBrowser(baseUrl )
        val client: JBCefClient =  browser.jbCefClient ;
        // 返回其作为 JComponent 的视图
        // 创建并设置自定义的 JBCefClient
//        val handler = object : JBCefOsrHandlerBrowser ()
//
//
//
//        client.addLoadHandler(handler, browser.cefBrowser)
        // 返回 JBCefBrowser 组件作为 JComponent
        return browser.component
    }
}
class test : ToolWindowFactory {
    override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
        val webView = MyWebView()
        val component: JComponent = webView.createComponent()
        // 将 JBCefBrowser 组件添加到工具窗口的内容中
        toolWindow.component.add(component)
    }
}

class OpenJcefToolWindowAction  : AnAction() {
    override fun actionPerformed(e: AnActionEvent) {
        val project = e.project ?: return
        val toolWindow = ToolWindowManager.getInstance(project).getToolWindow("MyJcefToolWindow")
        toolWindow?.show()
    }
}